/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package crawler;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Putra
 */
public class Crawling {

    public List<String> listUrl = new ArrayList<String>();
    String targetUrl, startMark;
    GetHTML getHTML;
    String htmlText;

    public Crawling(String baseUrl, String targetUrl, String startMark) {
        this.targetUrl = targetUrl;
        this.startMark = startMark;
        this.getHTML = new GetHTML(baseUrl,targetUrl);

        Thread thread = new Thread(getHTML);
        thread.run();
        this.htmlText = getHTML.getHtmlText();
        String[] temp = this.htmlText.split("<div class=\"jr-masonry-results jrResults\">");
        this.htmlText = temp[1];
        getLink();
        
    }

    public Crawling(String targetUrl) {

    }
    

    private void getLink() {
        int index = htmlText.indexOf(this.startMark);
        while (index != -1) {
            String link = extractLink(index);
            if(!this.listUrl.contains(link)){
                this.listUrl.add(link);
                //addTojList(link);
                
            }
            index = htmlText.indexOf(this.startMark, index + 1);
        }
        
    }
    
    private String extractLink(int indexStart){
        String link = new String();
        int index = indexStart;
        while(htmlText.charAt(index)!=' ' && htmlText.charAt(index)!='>'){
            link=link+htmlText.charAt(index);
            index++;
        }
        link = cleaning(link,"href=");
        if(link.indexOf(this.getHTML.getBaseUrl()) == -1) link = this.getHTML.getBaseUrl()+link;
        return link;
    }
    
    private String cleaning(String link,String target){
        String ret = link.replaceAll(target,"");
        ret = ret.substring(1, ret.length()-1);
        ret.trim();
        return ret;
    }

    
}
